// 权限表

const rule = require("../../model/user/rule")
const { getTime } = require("../../utils/gettime")

const {
    handleNumber
} = require("../../rules/verification")

function findOne(data) {
    let res = rule.findOne({
        attributes: ["id", "rulename", "summary", "createdate", "creator", "updated", "updator"],
        where: data
    })
    return res
}

// 只获取权限id 权限名称
exports.getName = async () => {
    
    // try {
        let res = await rule.findAll({attributes:["rulename","id"]})
        if (res) {
            return { msg: "查询成功", code: "200", data: res }
        }
        return { msg: "查询失败", code: "400" }
    // } catch (err) { return { msg: "查询失败", code: "400", err } }
}

// 根据 id 查询权限 
exports.getById = async ({ id }) => {
    try {
        let res = await findOne({ id })
        if (res) {
            return { msg: "查询成功", code: "200", data: res }
        }
        return { msg: "查询失败", code: "400" }
    } catch (err) { return { msg: "查询失败", code: "400", err } }
}
// 获取权限列表    
exports.getList = async ({ pageNum, pageSize }) => {
    try {
        if (!pageNum) {
            let res = await rule.findAll({
                attributes: ["id", "summary", "rulename"]
            })
            if (res) return { msg: "查询成功", code: "200", data: res }
            return { msg: "查询失败", code: "400" }
        } else {
            let checkedNum = handleNumber(pageNum, "pageNum")
            if (checkedNum.code !== "200") return checkedNum
            let checkedSize = handleNumber(pageSize, "pageSize")
            if (checkedSize.code !== "200") return checkedSize
            let res = await rule.findAndCountAll({
                offset: checkedNum.data * checkedSize.data - checkedSize.data,
                limit: checkedSize.data,
                attributes: ["id", "rulename", "summary", "creator", "createdate"]
            })
            if (res) return { msg: "查询成功", code: "200", data: res }
            return { msg: "查询失败", code: "400" }
        }
    } catch (err) { return { msg: "查询失败", code: "400", err } }
}
// 添加权限
exports.create = async ({ rulename, summary, creator }) => {
    try {
        let isCreate = await rule.findOne({ where: { rulename } })
        if (isCreate) return { msg: "重复的权限名", code: "400" }
        let res = await rule.create({ rulename, summary, creator, createdate: getTime() })
        if (!res) return { msg: "添加失败", code: "400" }
        return { msg: "添加成功", code: "200" }
    } catch (err) { return { msg: "添加失败", code: "400", err } }
}
// 编辑权限
exports.edit = async ({ rulename, summary, updator, id }) => {
    try {
        id = Number(id)
        let newData = {}
        if (summary) newData.summary = summary;
        if (updator) newData.updator = updator;
        if (rulename) {
            let isRulename = await rule.findOne({ where: { rulename }, attributes: ["id"] })
            if (isRulename) {
                if (id !== isRulename.id) return { msg: "重复的权限名称", code: "400" }
            } else {
                newData.rulename = rulename;
            }
        }
        newData.updated = getTime()
        let res = await rule.update(newData, { where: { id } })
        if (res) return { msg: "编辑成功", code: "200" }
        return { msg: "编辑失败", code: "400" }
    } catch (err) { return { msg: "编辑失败", code: "400", err } }
}
// 删除权限
exports.remove = async ({ id }) => {
    try {
        let isRemove = await rule.findOne({ where: { id } })
        if (!isRemove) return { msg: "删除失败 需要删除的权限不存在", code: "400" }
        let res = await rule.destroy({ where: { id } })
        if (res) return { msg: "删除成功", code: "200" }
        return { msg: "删除失败", code: "400" }
    } catch (err) { return { msg: "删除失败", code: "400", err } }
}